//
//  LGCareMonitorEditCell.m
//  HeartDiseaseManage
//
//  Created by Bovin on 2024/8/4.
//

#import "LGCareMonitorEditCell.h"

@interface LGCareMonitorEditCell()
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *selectBtn;
/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UILabel *rateTitle;
@property (nonatomic , strong)UILabel *rateLabel;
@property (nonatomic , strong)UIImageView *rateTip;
/**   */
@property (nonatomic , strong)UILabel *timeTitle;
@property (nonatomic , strong)UILabel *timeLabel;
@property (nonatomic , strong)UIImageView *timeTip;

@end

@implementation LGCareMonitorEditCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.type = LGPositionTypeSingle;
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:LGNSString(dataDic[@"icon"])]];
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    self.selectBtn.selected = [dataDic[@"selected"] boolValue];
    NSString *rateType = LGNSString(dataDic[@"frequency"][@"type"]);
    NSString *number = LGNSString(dataDic[@"frequency"][@"nums"]);
    NSArray *rateArray = dataDic[@"frequencySelect"];
    self.rateLabel.text = @"请选择";
    for (NSDictionary *tempDic in rateArray) {
        NSString *value = LGNSString(tempDic[@"value"]);
        NSString *title = LGNSString(tempDic[@"title"]);
        if([rateType isEqualToString:value]){
            self.rateLabel.text = (number.length>0)?[NSString stringWithFormat:@"%@%@天",title,number]:title;
            break;
        }
    }
   
    NSString *content = @"";
    NSArray *selectTimes = dataDic[@"times"];
    NSArray *timeArray = dataDic[@"timeSelect"];
    for (NSDictionary *tempDic in timeArray) {
        NSString *value = LGNSString(tempDic[@"value"]);
        NSString *title = LGNSString(tempDic[@"title"]);
        if([selectTimes containsObject:value]){
            content = (content.length>0)?[NSString stringWithFormat:@"%@,%@",content,title]:title;
        }
    }
    content = (content.length>0)?content:@"请选择";
    self.timeLabel.text = content;
}

-(void)selecteBtnAciton:(UIButton *)sender{
    sender.selected = !sender.selected;
    if(self.delegate && [self.delegate respondsToSelector:@selector(careMonitorSelect:index:)]){
        [self.delegate careMonitorSelect:sender.selected index:self.index];
    }
}

-(void)createSubView{
    [self.baseView addSubview:self.iconView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.selectBtn];
    [self.baseView addSubview:self.lineView];
    [self.baseView addSubview:self.rateTitle];
    [self.baseView addSubview:self.rateLabel];
    [self.baseView addSubview:self.rateTip];
    [self.baseView addSubview:self.timeTitle];
    [self.baseView addSubview:self.timeLabel];
    [self.baseView addSubview:self.timeTip];
    [self.rateTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.centerY.equalTo(self.rateLabel);
    }];
    [self.rateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(26));
        make.left.equalTo(self.baseView).offset(viewPix(95));
    }];
    [self.rateTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.rateLabel);
        make.width.height.equalTo(@(viewPix(12)));
    }];
    
    [self.timeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.centerY.equalTo(self.timeLabel);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.rateLabel.mas_bottom).offset(viewPix(12));
        make.left.right.equalTo(self.rateLabel);
        make.bottom.equalTo(self.baseView).offset(-viewPix(12));
    }];
    [self.timeTip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.timeLabel);
        make.width.height.equalTo(@(viewPix(12)));
    }];
    
}

-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(12), viewPix(24), viewPix(24))];
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(42), viewPix(12), Screen_W-viewPix(120), viewPix(24));
    }
    return _titleLabel;
}

-(UIButton *)selectBtn{
    if(!_selectBtn){
        _selectBtn = [UIButton buttonWithImage:@"unselect20" selectImage:@"selected20"];
        _selectBtn.frame = CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(48));
        _selectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _selectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(12));
        [_selectBtn addTarget:self action:@selector(selecteBtnAciton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(48), Screen_W-viewPix(56), 1)];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
    }
    return _lineView;
}

-(UILabel *)rateTitle{
    if(!_rateTitle){
        _rateTitle = [UILabel lableWithText:@"测量频率" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _rateTitle;
}

-(UILabel *)rateLabel{
    if(!_rateLabel){
        _rateLabel = [UILabel lableWithText:@"请选择" colorString:@"#909399" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:0];
    }
    return _rateLabel;
}

-(UIImageView *)rateTip{
    if(!_rateTip){
        _rateTip = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
    }
    return _rateTip;
}

-(UILabel *)timeTitle{
    if(!_timeTitle){
        _timeTitle = [UILabel lableWithText:@"测量时间" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _timeTitle;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"请选择" colorString:@"#909399" textFont:LGFont(16) textAlignment:NSTextAlignmentRight lines:0];
    }
    return _timeLabel;
}

-(UIImageView *)timeTip{
    if(!_timeTip){
        _timeTip = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
    }
    return _timeTip;
}

@end
